using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MusicPlayer;
using TagReader;

using System.Runtime.InteropServices;

namespace SimpleMusicPlayer
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		// LOCAL VARS
		private int _stream = 0;

		private Player player;

		private Label label;
		private Label infoLabel;
 
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			player = new Player();
			label = new Label();
			infoLabel = new Label();
			label.Dock = DockStyle.Top;
			infoLabel.Dock = DockStyle.Bottom;
			this.Controls.Add(label);
			this.Controls.Add(infoLabel);
			player.PercentageCompleted += new PercentageCompletedDelegate(player_PercentageCompleted);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AllowDrop = true;
			this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
			this.ClientSize = new System.Drawing.Size(480, 243);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Click += new System.EventHandler(this.Form1_Click);
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
			this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			StopPlayback();
			string _fileName = ((string[]) e.Data.GetData("FileNameW",true))[0];
			if (_fileName != String.Empty)
			{
				_stream = player.PlayMusic(_fileName);
				TagReader.TagReader reader = new TagReader.TagReader();
				TagReader.TrackInfo info =  reader.ReadTrackInfoFile(_fileName);
				this.infoLabel.Text = string.Format("{0} - {1} ({2})", info.ArtistName, info.TrackTitle, info.AlbumTitle);
			}
		}

		private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			if (e.Data.GetDataPresent("FileNameW")) 
				e.Effect = DragDropEffects.Move;
			else
				e.Effect = DragDropEffects.None;
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{

		}

		private bool StopPlayback() 
		{
			if (_stream != 0) 
			{
				player.Stop();
				return true;
			}
			else 
				return false;
		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			StopPlayback();
		}

		private void Form1_Click(object sender, System.EventArgs e)
		{
			StopPlayback();
			_stream = 0;
		}

		private void player_PercentageCompleted(double percentage)
		{
			this.label.Text = string.Format("now: {0}, percentage completed: {1}%.", DateTime.Now, percentage * 100);
		}
	}
}
